package com.ruoyi.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.mapper.EWalletMapper;
import com.ruoyi.business.domain.EWallet;
import com.ruoyi.business.service.IEWalletService;

/**
 * 钱包管理Service业务层处理
 *
 * @author hao
 * @date 2023-02-01
 */
@Service
public class EWalletServiceImpl extends ServiceImpl<EWalletMapper, EWallet> implements IEWalletService {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询钱包管理列表
     *
     * @param eWallet 钱包管理
     * @return 钱包管理
     */
    @Override
    public List<EWallet> selectEWalletList(EWallet eWallet) {
        List<EWallet> list = getBaseMapper().selectEWalletList(eWallet);
        for (EWallet wallet : list) {
            SysUser byAllId = sysUserService.getByAllId(wallet.getUserId());
            wallet.setUser(byAllId);
        }
        return list;
    }

    @Override
    public Boolean saves(Long userId) {
        SysUser byAllId = sysUserService.getByAllId(userId);
        EWallet eWallet = new EWallet();
        eWallet.setUserId(userId);
        eWallet.setState(1L); // 设置默认值
        eWallet.setUser(byAllId);
        return super.save(eWallet);
    }


    /**
     * 冻结
     *
     * @param id
     */
    @Override
    public void dongjieById(Long id) {
        LambdaQueryWrapper<EWallet> wrapper = Wrappers.<EWallet>lambdaQuery()
                .eq(EWallet::getId, id);
        EWallet eWallet = getBaseMapper().selectOne(wrapper);
        if (eWallet.getState() == 1L) {
            eWallet.setState(0L);
        } else {
            eWallet.setState(1L);
        }
        update(eWallet, wrapper);


    }


}
